package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dto.ArticleDTO;
import com.itheima.dto.ArticlePageDTO;
import com.itheima.entity.Article;
import com.itheima.entity.YddArticle;
import com.itheima.mapper.YddArticleMapper;
import com.itheima.service.YddArticleService;
import com.itheima.vo.PageInfoVO;
import com.itheima.vo.YddArticleVO2;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class YddArticleServiceImpl implements YddArticleService {
    private final YddArticleMapper yddArticleMapper;


    /**
     * 文章列表-分页
     *
     * @param articlePageDTO
     * @return {@link PageInfoVO}<{@link YddArticle}>
     */
    @Override
    public PageInfoVO<YddArticleVO2> list(ArticlePageDTO articlePageDTO) {
        //todo 1.开启
        PageHelper.startPage(articlePageDTO.getPage(),articlePageDTO.getLimit());
        //todo 2.原始查询
        List<YddArticle> yddArticles = yddArticleMapper.selectByPageInfo(articlePageDTO.getTitle(),articlePageDTO.getCateid(),articlePageDTO.getStart_time(),articlePageDTO.getEnd_time());
        List<YddArticleVO2> yddArticleVO2s = yddArticles.stream().map(new Function<YddArticle, YddArticleVO2>() {
            @Override
            public YddArticleVO2 apply(YddArticle yddArticle) {
                YddArticleVO2 yddArticleVO2 = new YddArticleVO2();
                BeanUtils.copyProperties(yddArticle, yddArticleVO2);
                yddArticleVO2.setCreate_date(yddArticle.getCreateDate());
                yddArticleVO2.setUpdate_date(yddArticle.getUpdateDate());
                return yddArticleVO2;
            }
        }).collect(Collectors.toList());
        //todo 3.数据转换
        PageInfo<YddArticleVO2> pageInfo = PageInfo.of(yddArticleVO2s);
        //todo 4.封装数据返回
        PageInfoVO<YddArticleVO2> yddArticlePageInfoVO = new PageInfoVO<>();
        yddArticlePageInfoVO.setPageSize(articlePageDTO.getLimit());
        yddArticlePageInfoVO.setTotalPages(pageInfo.getPages());
        yddArticlePageInfoVO.setCurrentPage(pageInfo.getPageNum());
        yddArticlePageInfoVO.setCount(pageInfo.getTotal());
        yddArticlePageInfoVO.setData(yddArticleVO2s);
        return yddArticlePageInfoVO;
    }

    /**
     * 添加文章
     *
     * @param article
     */
    @Override
    public void save(Article article) {
        LocalDateTime localDateTime = LocalDateTime.now();
        // 将 LocalDateTime 转换为特定时区的 ZonedDateTime
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneOffset.systemDefault());
        // 将 ZonedDateTime 转换为 Instant
        Instant instant = zonedDateTime.toInstant();
        // 获取时间戳（以毫秒为单位）
        long createDate = instant.toEpochMilli();
        article.setCreateDate(createDate);
        article.setUpdateDate(createDate);

        article.setStatus(2);
        article.setDefaultData("1");
        article.setUserId(136);

        yddArticleMapper.insert(article);
        //更新ydd_user_data 表
        int type = 1;
        yddArticleMapper.insertUserAndData(article.getId(),article.getUserId(),type);
    }

    /**
     * 回显
     *
     * @param id
     * @return {@link YddArticle}
     */
    @Override
    public YddArticleVO2 rowInfo(Long id) {
        YddArticle yddArticle = yddArticleMapper.selectById(id);
        YddArticleVO2 yddArticleVO2 = new YddArticleVO2();
        BeanUtils.copyProperties(yddArticle,yddArticleVO2);
        yddArticleVO2.setCreate_date(yddArticle.getCreateDate());
        yddArticleVO2.setUpdate_date(yddArticle.getUpdateDate());
        return yddArticleVO2;
    }

    /**
     * 文章修改
     *
     * @param articleDTO
     */
    @Override
    public void update(ArticleDTO articleDTO) {
        Article article = new Article();
        BeanUtils.copyProperties(articleDTO,article);
        article.setCreateDate(articleDTO.getCreate_date());
        yddArticleMapper.update(article);
    }

    /**
     * 删除文章
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        yddArticleMapper.delete(id);
    }

}
